package com.yuntsg.nnsfcp.controller;

import com.yuntsg.nnsfcp.common.beans.ResultBean;
import com.yuntsg.nnsfcp.common.utils.CheckUtil;
import com.yuntsg.nnsfcp.common.utils.StringUtil;
import com.yuntsg.nnsfcp.common.utils.UserUtil;
import com.yuntsg.nnsfcp.front.entity.BidDocumentArticle;
import com.yuntsg.nnsfcp.front.mapper.BidDocumentArticleMapper;
import com.yuntsg.nnsfcp.service.CommonService;
import com.yuntsg.nnsfcp.service.front.impl.UpLoadServiceImpl;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author: 葛成瑞
 * @Date: 2024/9/26 8:12
 */
//@Api(value = "前后台共用请求")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    CommonService commonService;

    @Resource
    UpLoadServiceImpl UpLoadServiceImpl;


    @Resource
    BidDocumentArticleMapper bidDocumentArticleMapper;

    //@ApiOperation(value = "输入路径下载文件")
//    @ApiImplicitParam(value = "文件路径", name = "path", required = true, dataType = "String")
    @GetMapping("/download")
    public void downLoad(String path) {
        commonService.commonDown(path);
    }

    @GetMapping("/download-real")
    public void downLoadReal(String path) {
        //传递真实路径进行下载使用
        commonService.commonDownReal(path);
    }

    //@ApiOperation(value = "输入路径下载文件报告专用下载")
//    @ApiImplicitParams({@ApiImplicitParam(value = "文件路径", name = "path", required = true, dataType = "String"), @ApiImplicitParam(value = "文件路径", name = "path", required = true, dataType = "String")})
    @GetMapping("/download-report")
    public void downLoad(Integer id, String path) {
        CheckUtil.check(StringUtil.isNotNull(id), "id 不可为空");
        BidDocumentArticle bidDocumentArticle = bidDocumentArticleMapper.selectById(id);
        CheckUtil.check(StringUtil.isNotNull(bidDocumentArticle), "非法请求");
        if (path.equals(bidDocumentArticle.getTxtWord())) {
            bidDocumentArticle.setTxtWordDf(2);
        } else {
            bidDocumentArticle.setPicWordDf(2);
        }
        bidDocumentArticleMapper.updateById(bidDocumentArticle);
        commonService.commonDown(path);
    }

    /**
     * 上传附件
     *
     * @param file 文件
     * @return 路径/文件名
     * @throws Exception
     */
    @PostMapping("/uploadFile")
    //@ApiOperation(value = "文件上传", notes = "上传附件 返回路径")
    public ResultBean<String> uploadFile(@RequestPart("file") MultipartFile file) throws Exception {
        //文件上传到服务器,返回  路径/文件名
        String path = UpLoadServiceImpl.upLoad(file);
        CheckUtil.notNull(path);
        return new ResultBean<>(path);
    }

}
